package 路径总和II113;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 谢阳
 * @version 1.8.0_131
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    private List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {

        Deque<Integer> deque = new LinkedList<>();
        dfs(root, targetSum, deque);
        return res;
    }

    private void dfs(TreeNode node, int sum, Deque<Integer> deque) {
        if (node == null) {
            return;
        }
        deque.addLast(node.val);
        sum -= node.val;
        if (node.left == null && node.right == null && sum == 0) {
            res.add(new LinkedList<>(deque));
        }

        dfs(node.left, sum, deque);
        dfs(node.right, sum, deque);

        deque.removeLast();
    }
}
